package com.oywy.rbac.controller;

import com.oywy.rbac.entity.User;
import com.oywy.rbac.service.UserService;
import com.oywy.rbac.vo.ApiDataGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理控制器
 *
 * @author oywy
 * @date 2018/10/22
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public ApiDataGridResult<User> list() {
        List<User> users = userService.listUser();
        return new ApiDataGridResult<>(users.size(), users);
    }

    @GetMapping("{id}")
    public User get(@PathVariable String id) {
        return userService.get(id);
    }

    @PostMapping
    public User post(User user) {
        return userService.post(user);
    }

    @PostMapping("{id}/role")
    public ResponseEntity grantRoles(@PathVariable("id") String id, @RequestParam("roleIds") List<String> roleIds) {
        userService.grantRole(id, roleIds);
        return ResponseEntity.ok().build();
    }

    @PutMapping
    public User put(User user) {
        return userService.put(user);
    }

    @DeleteMapping("{id}")
    public void del(@PathVariable String id) {
        userService.del(id);
    }
}
